<?php
namespace app\zhonghang\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
require_once (APP_PATH . 'api/RewardApi.php');
class Person extends Base {
    public function _initialize(){
        parent::_initialize();
       
    }
   
    //获取官方邮箱
    public function gfmail(){
        $list=DB::name('kefu')->where('type','mail')->select();
        if($list){
            $data['info']='官方邮箱';
            $data['status']='1';
            $data['data']=$list;
            return json($data);
        }else{
            $data['info']='无数据';
            $data['status']='-1';
            $data['data']=array();
            return json($data);
        }
    }

    //更换头像
    public function replacePicture(){
        $member=$this->member;
        $pic=input('pic');

        //上传头像
        $imageName = rand(1000,100000).time().'.png';
        if (strstr($pic,",")){
            $image = explode(',',$pic);
            $image = $image[1];
        }
        $path = "/public/uploads/memberpic";
        $imageSrc= $path."/". $imageName; //图片名字
        $r = file_put_contents(ROOT_PATH.$imageSrc, base64_decode($image));
        $relation['pic']=$imageSrc;

        $res=Db::name('app_member_relation')->where(array('member_id'=>$member['member_id']))->update($relation);
        if($res){
            $data['info']='修改成功';
            $data['status']='1';
            return json($data);
        }else{
            $data['info']='修改失败';
            $data['status']='-1';
            return json($data);
        }
    }

    public function index(){
    	$member=$this->member;
    	$uid=$this->member['member_id'];
    	
    	$relation=Db::name('app_member_relation')->where(array('member_id'=>$uid))->find();
        if($relation['id_authentication']==0){
            $list['relation']['name']='未认证';
        }else{
            $list['relation']['name']=$relation['name'];
        }
        $list['relation']['pic']="/public/uploads/touxiang.png";
    	if($relation['zhuce_id']=='0'){
    		$list['code_status']='1';
    	}else{
    		$list['code_status']='0';
    	}

    	//获取未读消息数
    	$message=Db::name('message')->where(array('to_member_id'=>$uid))->where(array('status'=>'0'))->count();
    	if($message>0){
    		$list['message_status']='1';
    	}else{
    		$list['message_status']='0';
    	}

    	$data['info']='获取成功';
    	$data['status']='1';
    	$data['data']=$list;
    	return json($data);


    }


    public function writeCode(){
    	$member=$this->member;
    	$code=input('code');
    	//获取邀请人信息
    	$inviter=Db::name('app_member_relation')->where(array('code'=>$code))->find();
    	if(!$inviter){
    		$data['info']='无效的邀请码';
    		$data['status']='-1';
    		$data['data']=array();
    		return json($data);
    	}
    	// echo "<pre>";
    	// echo $inviter['member_id'];
    	// die();
    	//填写邀请人
    	$relation['zhuceid']=$inviter['member_id'];
    	$res=Db::name('app_member_relation')->where(array('member_id'=>$member['member_id']))->update($relation);

    	if($res){
    		$data['info']='填写成功';
    		$data['status']='1';
    		$data['data']=array();
    		return json($data);
    	}else{
    		$data['info']='填写失败';
    		$data['status']='-1';
    		$data['data']=array();
    		return json($data);
    	}
    }

    //获取订单
    public function Orders(){


    	$member=$this->member;
    	$status=input('delivery');

		 if(empty($status)&&$status!='0'){
            $list=Db::name('orders')->field('id,delivery,total')->where(array('member_id'=>$member['member_id']))->select();
			
		}else{
            $list=Db::name('orders')->field('id,delivery,total')->where(array('member_id'=>$member['member_id']))->where(array('delivery'=>$status))->select();
		}
	

		foreach ($list as $key => $value) {
			$list[$key]['detail']=Db::name('order_detail')->field('good_name,price,address,day_play as num,pic')->where(array('order_id'=>$value['id']))->select();


            foreach ($list[$key]['detail'] as $k => $v) {
                //获取地址
                $address=explode(',',$v['address']);
            }
		}
    	if($list){
    		$data['info']='获取成功';
    		$data['status']='1';
    		$data['data']=$list;
    		return json($data);	
    	}else{
    		$data['info']='暂无数据';
    		$data['status']='-1';
    		$data['data']=array();
    		return json($data);
    	}
    }


    //我的消息
    public function myMessage(){
    	$member=$this->member;
    	$list=Db::table('wst_message')->field('id,status,title,content,add_time')->where(array('to_member_id'=>$member['member_id']))->select();

    	if($list){
    		$data['info']='获取成功';
    		$data['status']='1';
    		$data['data']=$list;
    		return json($data);
    	}else{
    		$data['info']='暂无消息';
    		$data['status']='1';
    		$data['data']=array();
    		return json($data);
    	}
    }

    //订单详情
    public function orderInfo(){
    	//获取订单id
    	$id=input('id');

    	$list=Db::name('orders')->field('id,delivery,total,fee,num as equipment,add_time,total,discount,contract_id,invoice,number as order_code,pay_way')->where(array('id'=>$id))->find();
        if($list['invoice'] != 0){
            $fapiao = DB::name('fapiao')->where('id',$list['invoice'])->value('type');
            if($fapiao==1){
                $list['invoice']='个人发票';
            }else{
                $list['invoice']='企业发票';
            }
        }else{
            $list['invoice']='未开发票';
        }
    	$list['detail']=Db::name('order_detail')->field('good_name,price,address,pic,play_time,gid,template_id,day_play as num')->where(array('order_id'=>$list['id']))->select();
    	foreach($list['detail'] as $k => $v){
            $arr=explode('-',$v['play_time']);
            $list['detail'][$k]['start_time']=$arr['0']/1000;
            $list['detail'][$k]['end_time']=$arr['1']/1000;
        }
        // $list[]
        $list['kaihuhang'] = DB::name('app_config')->where('key',shoukuan)->value('value');
        $list['shoukuannum'] = DB::name('app_config')->where('key',shoukuannum)->value('value');
        $list['shibiehao'] = DB::name('app_config')->where('key',shibiehao)->value('value');
        $list['mingcheng'] = DB::name('app_config')->where('key',mingcheng)->value('value');
        $list['dizhi'] = DB::name('app_config')->where('key',dizhi)->value('value');
        $list['dianhua'] = DB::name('app_config')->where('key',dianhua)->value('value');
		if($list){
			$data['info']='获取成功';
			$data['status']='1';
			$data['data']=$list;
			return json($data);
		}else{
			$data['info']='获取失败';
			$data['status']='-1';
			$data['data']=array();
			return json($data);
		}
    }


    //删除订单
    public function delOrder(){
        //获取订单id
        $id=input('id');

        $res=Db::name('orders')->where(array('id'=>$id))->delete();
        Db::name('order_detail')->where(array('order_id'=>$id))->delete();
        if($res){
            $data['info']='删除成功';
            $data['status']='1';
            $data['data']=array();
            return json($data);
        }else{
            $data['info']='删除失败';
            $data['status']='-1';
            $data['data']=array();
            return json($data);
        }
    }

    //签署合同
    public function signed_approve(){
        $member=$this->member;
        $type=input('type');
        if($type==''){
            $type='1';
        }

        switch ($type) {
            case '1':
                // echo "个人合同";
                $list['idcard']=$member['idcard'];
                $list['name']=$member['name'];
                $list['z_card']=$member['z_card'];
                $list['f_card']=$member['f_card'];
                $data['info']='获取成功';
                $data['status']='1';
                $data['data']=$list;
                return json($data);
                break;
            case '2':
                //经办人
                $list['operator']=$member['operator'];
                //公司名称
                $list['team_name']=$member['team_name'];
                $list['operator_zcard']=$member['operator_zcard'];
                $list['operator_fcard']=$member['operator_fcard'];
                //营业执照
                $list['license_pic']=$member['license_pic'];
                $data['info']='获取成功';
                $data['status']='1';
                $data['data']=$list;
                return json($data);
                break;
        }
        
    }


    public function signature(){
        $member=$this->member;
        $image=input('pic');
        // echo "<pre>";
        // var_dump($image);
        // die();
                $imageName = rand(1000,100000).time().'.png';
                            if (strstr($image,",")){
                                $image = explode(',',$image);
                                $image = $image[1];
                            }
                            $path = "/public/uploads/real";
                            $imageSrc= $path."/". $imageName; //图片名字
                            $r = file_put_contents(ROOT_PATH.$imageSrc, base64_decode($image));
                            $arr['sign_pic']=$imageSrc;

        $arr['order_id']=input('order_id');
        $arr['member_id']=$member['member_id'];
        $type=input('type');
        $arr['type']=$type;
        Db::name('sign_name')->where(array('order_id'=>$arr['order_id']))->delete();
        Db::name('sign_name')->insert($arr);

        $data['info']='合同信息已提交,请等待审核';
        $data['status']='1';
        $data['data']=array();
        return json($data);
    }

    //获取合同
    public function getcontract(){
        $order_id=input('order_id');
        $shijian=DB::name('order_detail')->where('order_id',$order_id)->value('play_time');
        $shuliang=DB::name('orders')->where('id',$order_id)->value('num');
        $feiyong=DB::name('orders')->where('id',$order_id)->value('fee');
        $value=Db::name('app_config')->where(array('key'=>'real_name'))->value('value');
        $value1=str_replace('111',$shijian,$value);
        $value2=str_replace('222',$shuliang,$value1);
        $value3=str_replace('333',$feiyong,$value2);
        $list['contract']=$value3;
        // $list['order_id']=$order_id;

        $data['info']='获取成功';
        $data['status']='1';
        $data['data']=$list;
        return json($data);
    } 

    //个人企业实名认证
    public function signed_next(){
       $type = input('type');
       $member=$this->member;
       switch ($type) {

           case '1'://个人合同
               
                            $name=input('name');
                            $z_card=input('z_card');
                            $f_card=input('f_card');
                            $idcard=input('idcard');
                            // echo "<pre>";
                            // var_dump($z_card);
                            // die();
                            if(!$name){
                                $data['info']='姓名不能为空';
                                $data['status']='-1';
                                return json($data);
                            }

                            if(!$idcard){
                                $data['info']='身份证号不能为空';
                                $data['status']='-1';
                                return json($data);
                            }


                            //获取姓名
                            $relation['name']=$name;
                            //获取身份证号
                            $relation['idcard']=$idcard;
                            //上传正面
                            if(strlen($z_card)>200) {
                                $imageName = 'z' . rand(1000, 100000) . time() . '.png';
                                if (strstr($z_card, ",")) {
                                    $image = explode(',', $z_card);
                                    $image = $image[1];
                                }
                                $path = "/public/uploads/real";
                                $imageSrc = $path . "/" . $imageName; //图片名字
                                // echo "<pre>";
                                // var_dump($image);
                                // die();
                                $r = file_put_contents(ROOT_PATH . $imageSrc, base64_decode($image));
                                $relation['z_card'] = $imageSrc;
                            }

                            if(strlen($f_card)>200) {
                                //上传反面
                                $imageName = 'f' . rand(1000, 100000) . time() . '.png';
                                if (strstr($f_card, ",")) {
                                    $image = explode(',', $f_card);
                                    $image = $image[1];
                                }
                                $path = "/public/uploads/real";
                                $imageSrc = $path . "/" . $imageName; //图片名字
                                $r = file_put_contents(ROOT_PATH . $imageSrc, base64_decode($image));
                                $relation['f_card'] = $imageSrc;
                            }
                            //修改信息
                            $res=Db::name('app_member_relation')->where(array('member_id'=>$member['member_id']))->update($relation);
                                $data['info']='获取成功';
                                $data['status']='1';
                                //获取合同
                                $value=Db::name('app_config')->where(array('key'=>'real_name'))->value('value');
                                $list['contract']=$value;
                                $data['data']=$list;
                                return json($data);



               
               break;

           case '2'://企业合同
              
                    $operator=input('name');
                    $team_name=input('team_name');
                    $license_pic=input('license_pic');
                    $operator_zcard=input('operator_zcard');
                    $operator_fcard=input('operator_fcard');

                    //判断不能为空
                    if(!$operator){
                        $data['info']='经办人姓名不能为空';
                        $data['status']='-1';
                        return json($data);
                    }

                    if(!$team_name){
                        $data['info']='企业名称不能为空';
                        $data['status']='-1';
                        return json($data);
                    }

                    if(!$license_pic){
                        $data['info']='营业执照不能为空';
                        $data['status']='-1';
                        return json($data);
                    }
//
//                    if(!$operator_zcard){
//                        $data['info']='身份证正面不能为空';
//                        $data['status']='-1';
//                        return json($data);
//                    }
//
//                    if(!$operator_fcard){
//                        $data['info']='身份证反面不能为空';
//                        $data['status']='-1';
//                        return json($data);
//                    }

                    //获取经办人姓名
                    $relation['operator']=$operator;
                    //获取企业名称
                    $relation['team_name']=$team_name;
                    if(strlen($operator_zcard)>200) {
                        //上传正面
                        $imageName = 'z' . rand(1000, 100000) . time() . '.png';
                        if (strstr($operator_zcard, ",")) {
                            $image = explode(',', $operator_zcard);
                            $image = $image[1];
                        }
                        $path = "/public/uploads/real";
                        $imageSrc = $path . "/" . $imageName; //图片名字
                        $r = file_put_contents(ROOT_PATH . $imageSrc, base64_decode($image));
                        $relation['operator_zcard'] = $imageSrc;
                    }

               if(strlen($operator_fcard)>200) {
                   //上传反面
                   $imageName = 'f' . rand(1000, 100000) . time() . '.png';
                   if (strstr($operator_fcard, ",")) {
                       $image = explode(',', $operator_fcard);
                       $image = $image[1];
                   }
                   $path = "/public/uploads/real";
                   $imageSrc = $path . "/" . $imageName; //图片名字
                   $r = file_put_contents(ROOT_PATH . $imageSrc, base64_decode($image));
                   $relation['operator_fcard'] = $imageSrc;
               }

               if(strlen($license_pic)>200) {
                   //上传营业执照
                   $imageName = 'l' . rand(1000, 100000) . time() . '.png';
                   if (strstr($license_pic, ",")) {
                       $image = explode(',', $license_pic);
                       $image = $image[1];
                   }
                   $path = "/public/uploads/real";
                   $imageSrc = $path . "/" . $imageName; //图片名字
                   $r = file_put_contents(ROOT_PATH . $imageSrc, base64_decode($image));
                   $relation['license_pic'] = $imageSrc;

               }
                    //修改信息
                    $res=Db::name('app_member_relation')->where(array('member_id'=>$member['member_id']))->update($relation);

                        $data['info']='获取成功';
                        $data['status']='1';
                        //获取合同
                        $value=Db::name('app_config')->where(array('key'=>'real_name'))->value('value');
                        $list['contract']=$value;
                        $data['data']=$list;
                        return json($data);

            
               break;
       }
       
    }


}